package com.dataknife;

import org.apache.log4j.Logger;

import com.dataknife.core.Event;
import com.dataknife.core.EventManager;
import com.dataknife.core.PluginType;
import com.dataknife.core.Process;
import com.dataknife.util.ConfigHandler;
import com.dataknife.util.ReflectionUtil;

public class DataKnife
{
  private static final Logger logger = Logger.getLogger(DataKnife.class);

  private static void start(){
    ConfigHandler conf = (ConfigHandler) ReflectionUtil.getInstance("com.dataknife.util.ConfigHandler");
    EventManager em = EventManager.getInstance();
    em.registerPlugins(conf);
    logger.info("register plugins finish.");
    Event event = new Event(conf,Process.INIT);
    em.invokeAll(event);
    Event timerEvent = new Event(conf,Process.SERVICE);
    em.invoke(timerEvent,PluginType.TIMER);
  }

  private static void stop(){
    EventManager em = EventManager.getInstance();
    Event event = new Event(null,Process.DESTORY);
    try {
      em.invokeAll(event);
    } catch (Exception e) {
      logger.error(e);
    }
    System.exit(0);
  }

  public static void main(String args[]) {
    start();
//    if (args.length == 0 || args.length > 1) {
//      logger.error("Invalid parameter.Please input the one parameter.");
//      System.exit(1);
//    } else {
//      if ("start".equalsIgnoreCase(args[0])) {
//        logger.info("Start up LogBook Agent.");
//        start();
//        logger.info("Started up LogBook Agent success.");
//      } else if("stop".equalsIgnoreCase(args[0])){
//        logger.info("Stop LogBook Agent.");
//        stop();
//      } else {
//        logger.warn("Invalid parameter.Please input start or stop parameter.");
//        System.exit(1);
//      }
//    }
  }

}
